﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using Global;
public partial class AddResturant : System.Web.UI.Page
{
    Restaurant m_Res;
    int iFlag;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string strType = Session["flag"].ToString();
            if (strType == "edit")
            {
                iFlag = 0;
               
                this.TBname.Text = Session["name"].ToString();
                this.TBaddress.Text = Session["address"].ToString();
                this.TBtel.Text= Session["tel"].ToString();
                this.TBtablenum.Text = Session["tablenum"].ToString();
                this.TBmax.Text = Session["max"].ToString();
                this.TBmin.Text = Session["min"].ToString();
                this.TBcity.Text = Session["city"].ToString();
                this.TBdesc.Text = Session["desc"].ToString();
                
             
            }
            else if (strType == "add")
            {
                iFlag = 1;
         
            }
            
        }



         
    }
    //
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("ManageRestaurant.aspx");
    }
    //add
    protected void Button1_Click(object sender, EventArgs e)
    {
        

        Restaurant res = new Restaurant();

        res.SetName(TBname.Text.ToString());
        res.SetAddress(TBaddress.Text.ToString());

        res.SetTel(TBtel.Text.ToString());
        res.SetTableNum(int.Parse(TBtablenum.Text.ToString()));
        res.SetConsumeMax(int.Parse(TBmax.Text.ToString()));
        res.SetConsumeMin(int.Parse(TBmin.Text.ToString()));
        res.SetDesc(TBdesc.Text.ToString());
        res.SetCity(TBcity.Text.ToString());

       // RestaurantManager ResManager = new RestaurantManager();

        CreateManager Creator = new CreateManager();
        BaseManager ResManager = Creator.GetManager(1);


        bool b = false ;
        if (Session["flag"].ToString() == "add")
        {
          //  b = ResManager.AddRestaurant(res);
            b = ResManager.AddObject(res);
        }
            //EDIT
        else if (Session["flag"].ToString() == "edit")
        {
            res.SetID(int.Parse(Session["id"].ToString()));
            //b = ResManager.UpdateRestaurant(res);
            b = ResManager.UpdateObject(res);
        }

       if (b)
       {
           Response.Write("<script >alert(\" 成功！\");</script>");

           //添加后跳转到管理员界面
           Response.Redirect("ManageRestaurant.aspx");
       }
       else
       {
           Response.Write("<script >alert(\" 失败！\");</script>");
           return;
       }


    }
}
